<?php
/**
 * Smarty Internal Plugin
 *
 * @package Smarty
 * @subpackage Cacher
 */

/**
 * Cache Handler API
 *
 * @package Smarty
 * @subpackage Cacher
 * @author Rodney Rehm
 */
abstract class Smarty_CacheResource_Custom extends Smarty_CacheResource {
	
	/**
	 * fetch cached content and its modification time from data source
	 *
	 * @param $id string
	 *       	 unique cache content identifier
	 * @param $name string
	 *       	 template name
	 * @param $cache_id string
	 *       	 cache id
	 * @param $compile_id string
	 *       	 compile id
	 * @param $content string
	 *       	 cached content
	 * @param $mtime integer
	 *       	 cache modification timestamp (epoch)
	 * @return void
	 */
	protected abstract function fetch($id, $name, $cache_id, $compile_id, &$content, &$mtime);
	
	/**
	 * Fetch cached content's modification timestamp from data source
	 *
	 * {@internal implementing this method is optional.
	 * Only implement it if modification times can be accessed faster than
	 * loading the complete cached content.}}
	 *
	 * @param $id string
	 *       	 unique cache content identifier
	 * @param $name string
	 *       	 template name
	 * @param $cache_id string
	 *       	 cache id
	 * @param $compile_id string
	 *       	 compile id
	 * @return integer boolean (epoch) the template was modified, or false if
	 *         not found
	 */
	protected function fetchTimestamp($id, $name, $cache_id, $compile_id) {
		return null;
	}
	
	/**
	 * Save content to cache
	 *
	 * @param $id string
	 *       	 unique cache content identifier
	 * @param $name string
	 *       	 template name
	 * @param $cache_id string
	 *       	 cache id
	 * @param $compile_id string
	 *       	 compile id
	 * @param $exp_time integer|null
	 *       	 seconds till expiration or null
	 * @param $content string
	 *       	 content to cache
	 * @return boolean success
	 */
	protected abstract function save($id, $name, $cache_id, $compile_id, $exp_time, $content);
	
	/**
	 * Delete content from cache
	 *
	 * @param $name string
	 *       	 template name
	 * @param $cache_id string
	 *       	 cache id
	 * @param $compile_id string
	 *       	 compile id
	 * @param $exp_time integer|null
	 *       	 seconds till expiration time in seconds or null
	 * @return integer number of deleted caches
	 */
	protected abstract function delete($name, $cache_id, $compile_id, $exp_time);
	
	/**
	 * populate Cached Object with meta data from Resource
	 *
	 * @param $cached Smarty_Template_Cached
	 *       	 cached object
	 * @param $_template Smarty_Internal_Template
	 *       	 template object
	 * @return void
	 */
	public function populate(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template) {
		$_cache_id = isset ( $cached->cache_id ) ? preg_replace ( '![^\w\|]+!', '_', $cached->cache_id ) : null;
		$_compile_id = isset ( $cached->compile_id ) ? preg_replace ( '![^\w\|]+!', '_', $cached->compile_id ) : null;
		
		$cached->filepath = sha1 ( $cached->source->filepath . $_cache_id . $_compile_id );
		$this->populateTimestamp ( $cached );
	}
	
	/**
	 * populate Cached Object with timestamp and exists from Resource
	 *
	 * @param $source Smarty_Template_Cached
	 *       	 cached object
	 * @return void
	 */
	public function populateTimestamp(Smarty_Template_Cached $cached) {
		$mtime = $this->fetchTimestamp ( $cached->filepath, $cached->source->name, $cached->cache_id, $cached->compile_id );
		if ($mtime !== null) {
			$cached->timestamp = $mtime;
			$cached->exists = ! ! $cached->timestamp;
			return;
		}
		$timestamp = null;
		$this->fetch ( $cached->filepath, $cached->source->name, $cached->cache_id, $cached->compile_id, $cached->content, $timestamp );
		$cached->timestamp = isset ( $timestamp ) ? $timestamp : false;
		$cached->exists = ! ! $cached->timestamp;
	}
	
	/**
	 * Read the cached template and process the header
	 *
	 * @param $_template Smarty_Internal_Template
	 *       	 template object
	 * @param $cached Smarty_Template_Cached
	 *       	 cached object
	 * @return booelan true or false if the cached content does not exist
	 */
	public function process(Smarty_Internal_Template $_template, Smarty_Template_Cached $cached = null) {
		if (! $cached) {
			$cached = $_template->cached;
		}
		$content = $cached->content ? $cached->content : null;
		$timestamp = $cached->timestamp ? $cached->timestamp : null;
		if ($content === null || ! $timestamp) {
			$this->fetch ( $_template->cached->filepath, $_template->source->name, $_template->cache_id, $_template->compile_id, $content, $timestamp );
		}
		if (isset ( $content )) {
			$_smarty_tpl = $_template;
			eval ( "?>" . $content );
			return true;
		}
		return false;
	}
	
	/**
	 * Write the rendered template output to cache
	 *
	 * @param $_template Smarty_Internal_Template
	 *       	 template object
	 * @param $content string
	 *       	 content to cache
	 * @return boolean success
	 */
	public function writeCachedContent(Smarty_Internal_Template $_template, $content) {
		return $this->save ( $_template->cached->filepath, $_template->source->name, $_template->cache_id, $_template->compile_id, $_template->properties ['cache_lifetime'], $content );
	}
	
	/**
	 * Empty cache
	 *
	 * @param $smarty Smarty
	 *       	 Smarty object
	 * @param $exp_time integer
	 *       	 expiration time (number of seconds, not timestamp)
	 * @return integer number of cache files deleted
	 */
	public function clearAll(Smarty $smarty, $exp_time = null) {
		$this->cache = array ();
		return $this->delete ( null, null, null, $exp_time );
	}
	
	/**
	 * Empty cache for a specific template
	 *
	 * @param $smarty Smarty
	 *       	 Smarty object
	 * @param $resource_name string
	 *       	 template name
	 * @param $cache_id string
	 *       	 cache id
	 * @param $compile_id string
	 *       	 compile id
	 * @param $exp_time integer
	 *       	 expiration time (number of seconds, not timestamp)
	 * @return integer number of cache files deleted
	 */
	public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time) {
		$this->cache = array ();
		return $this->delete ( $resource_name, $cache_id, $compile_id, $exp_time );
	}
	
	/**
	 * Check is cache is locked for this template
	 *
	 * @param $smarty Smarty
	 *       	 Smarty object
	 * @param $cached Smarty_Template_Cached
	 *       	 cached object
	 * @return booelan true or false if cache is locked
	 */
	public function hasLock(Smarty $smarty, Smarty_Template_Cached $cached) {
		$id = $cached->filepath;
		$name = $cached->source->name . '.lock';
		
		$mtime = $this->fetchTimestamp ( $id, $name, null, null );
		if ($mtime === null) {
			$this->fetch ( $id, $name, null, null, $content, $mtime );
		}
		
		return $mtime && time () - $mtime < $smarty->locking_timeout;
	}
	
	/**
	 * Lock cache for this template
	 *
	 * @param $smarty Smarty
	 *       	 Smarty object
	 * @param $cached Smarty_Template_Cached
	 *       	 cached object
	 */
	public function acquireLock(Smarty $smarty, Smarty_Template_Cached $cached) {
		$cached->is_locked = true;
		
		$id = $cached->filepath;
		$name = $cached->source->name . '.lock';
		$this->save ( $id, $name, null, null, $smarty->locking_timeout, '' );
	}
	
	/**
	 * Unlock cache for this template
	 *
	 * @param $smarty Smarty
	 *       	 Smarty object
	 * @param $cached Smarty_Template_Cached
	 *       	 cached object
	 */
	public function releaseLock(Smarty $smarty, Smarty_Template_Cached $cached) {
		$cached->is_locked = false;
		
		$id = $cached->filepath;
		$name = $cached->source->name . '.lock';
		$this->delete ( $name, null, null, null );
	}
}
?>